<?php
define('IN_TG','member_message');
define('IN_JS','member_message');
define('SCRIPT','member_message');
session_start();
require('includes/common.inc.php');
//加载头部文件
require('header.php');
//分页计算
$size = 10;
$sql="select tg_id,tg_fromuser,tg_content,tg_state,tg_date from tg_message where tg_touser ='{$_SESSION['username']}'";
_page($sql,$size);
$sql .= " order by tg_id limit $_pagenum,$size";
$res = _query($sql);
$message_arr = array();
while ($row = _fetch_array_list($res)) {
    $message_arr[] = $row;
}
if(!empty($_POST))
{
    if($_GET['action']=='delete')
    {
        if(isset($_POST['delete_ids']))
        {
            if (isset($_POST['ids']) && !empty($_POST['ids']) > 0) {
                $ids = implode(',', $_POST['ids']);
                $sql = "delete from tg_message where tg_id in ({$ids})";
                _query($sql);
            } else {
                _alert_back('请进行选择，再做进一步操作');
            }
        }
        else if(isset($_POST['state_ids']))
        {
            if (isset($_POST['ids']) && !empty($_POST['ids']) > 0) {
                $ids = implode(',', $_POST['ids']);
                $sql = "update tg_message set tg_state=1 where tg_id in ({$ids})";
                _query($sql);
            } else {
                _alert_back('请进行选择，再做进一步操作');
            }
        }
        header("location:member_message.php");
    }
}
?>
    <div id="member">
        <?php
        require ("includes/member.inc.php");
        ?>
        <div id="member_main">
            <h2>短信查阅</h2>
            <?php if (count($message_arr) > 0): ?>
            <form method="post" action="?action=delete">
                <table cellspacing="1">
                    <tr>
                        <th>发信人</th>
                        <th>留言内容</th>
                        <th>时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    <?php foreach ($message_arr as $row): ?>
                        <tr>
                            <td><?php echo $row['tg_fromuser'] ?></td>
                            <td title="<?php echo $row['tg_content'] ?>"><?php echo $row['tg_content'] ?></td>
                            <td><?php echo $row['tg_date'] ?></td>
                            <td><span
                                    style="color:blue;"><?php echo $row['tg_state'] == 1 ? "已阅读" : "未阅读"; ?></span>
                            </td>
                            <td><input name="ids[]" value="<?php echo $row['tg_id'] ?>" type="checkbox" class="ids"/>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <tr>
                        <td colspan="5">
                            <label for="all">全选 <input type="checkbox" name="chkall" id="all"/></label>
                            <input type="submit" value="批删除" name="delete_ids" id="delete_ids"/>
                            <input type="submit" value="批验证" name="state_ids" id="state_ids"/>
                        </td>
                    </tr>
                </table>
                <div id="page_text"><?php _paging(2) ?></div>
            </form>
            <?php else: ?>
                <h3 style="text-align:center;margin-top:20px;">暂无相关数据</h3>
            <?php endif; ?>

        </div>
    </div>

<?php
require('includes/footer.inc.php');
?>